<template>
  <div class="login">
    <img src="../img/login.png" alt />
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">登录</el-button>
        <!-- <el-button @click="resetForm('ruleForm')">注册</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";
import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {}
    };
  },
  created() {},
  methods: {
    //登录
    submitForm() {
      axios({
        url: "/api/login",
        method: "post",
        data: this.ruleForm
      }).then(res => {
        if (res.data.code) {
          window.localStorage.setItem("token", res.data.token);
          this.$router.push("/");
          localStorage.setItem("xhs", JSON.stringify(res.data.post));
          Toast.success(res.data.msg); //提示成功
        } else {
          Toast.fail(res.data.msg); //提示失败
        }
      });
    }
  }
};
</script>
<style lang="scss">
img {
  width: 100%;
  height: auto;
}
.demo-ruleForm {
  width: 300px;
}
</style>
